﻿<script type="text/javascript">
    $(document).ready(function () {
        $("#NewsLetter").keypress(function () {
            $("#EmailCadastrado").text('');
        });

        $("#CadastrarNewsLetter").click(function () {
            $("#EmailCadastrado").removeClass("error");
            
            var emailReg = @string.Concat(@"/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/", "");
            var email = $("#NewsLetter").val();

            if (email == null || email == '' || !emailReg.test(email)) {
                $("#EmailCadastrado").addClass("error");
                $("#EmailCadastrado").text("Email inválido");
                return;
            }

            $.ajax({ type: "POST",
                url: "/NewsLetter/CadastrarEmail",
                data: "email=" + $("#NewsLetter").val(),
                success: function (result) {
                    if (result.success) {
                        $("#EmailCadastrado").text(result.mensagem);
                        $("#NewsLetter").val("");
                    }
                },
                error: function (req, status, error) {
                    alert("Sorry! We could not receive your feedback at this time.");
                }
            });
        });
    });
</script>

<p>
    Para receber os Newsletters da Unijovem, preecha seu email abaixo:<br />
    <br />
    Email: @Html.TextBox("NewsLetter")
    <br />
    <label id="EmailCadastrado"></label>
    <br />
    <button id="CadastrarNewsLetter" title="Cadastrar">
        Cadastrar email</button>
</p>